package edu.ucsb.cs.cs185.supercam;

import android.app.Activity;
import android.app.DialogFragment;
import android.content.pm.ActivityInfo;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.Toast;

public class MainActivity extends Activity {

	FrameLayout layout;
	Camera mCamera;
	Preview mPreview;
	DialogFragment displayFragment;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        
        // requesting to turn the title OFF
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        // making it full screen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        		WindowManager.LayoutParams.FLAG_FULLSCREEN | 
        		WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        
        mCamera = null;
        mPreview = null;
    }
    
    public void Load(){
    	// Try to get the camera 
        
        // If the camera was received, create the app
        if (safeCameraOpen(0)){
        	/* Create our layout in order to layer the 
        	 * draw view on top of the camera preview. 
        	 */
            layout = new FrameLayout(this);
            layout.setLayoutParams(new LayoutParams(
            		LayoutParams.MATCH_PARENT,
            		LayoutParams.MATCH_PARENT));
            
            // Create a new camera view and add it to the layout
            mPreview = new Preview(this,mCamera);
            layout.addView(mPreview);
            
            // Set the layout as the apps content view 
            setContentView(layout);
        }
        // If the camera was not received, close the app
        else {
        	Toast toast = Toast.makeText(getApplicationContext(), 
        			"Unable to find camera. Closing.", Toast.LENGTH_SHORT);
        	toast.show();
        	finish();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    private boolean safeCameraOpen(int id) {
        boolean qOpened = false;
      
        try {
            releaseCameraAndPreview();
            mCamera = Camera.open(0);
            qOpened = (mCamera != null);
        } catch (Exception e) {
            Log.e(getString(R.string.app_name), "failed to open Camera");
            e.printStackTrace();
        }

        return qOpened;    
    }

    private void releaseCameraAndPreview() {
        if(mPreview != null){
        	mPreview.setCamera(null);
        }
        if (mCamera != null) {
            mCamera.release();
            mCamera = null;
        }
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        
        if (mPreview != null){
        	mPreview.onPause();
        	mPreview = null;
        }
        
    }
    
    @Override 
    protected void onResume(){
    	super.onResume();
    	
    	Load();
    }
    
    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case R.id.action_help:
	        	//toggle help overlay
	        	mPreview.f_help = !mPreview.f_help;
	            return true;
	        case R.id.action_cavg:
	        	mPreview.f_cavg = !mPreview.f_cavg;
	            return true;
	        case R.id.action_display:
	        	displayFragment = new CavgDialog();
	        	displayFragment.show(getFragmentManager(), "DisplaySettings");
	        	return true;
	        case R.id.action_about:
	        	DialogFragment afrag = new AboutFragment();
	        	afrag.show(getFragmentManager(), "About");
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
    
    public void setHexMode(View v) {
    	mPreview.f_mode = 0;
    	displayFragment.dismiss();
    }
    
    public void setRGBMode(View v) {
    	mPreview.f_mode = 1;
    	displayFragment.dismiss();
    }
    
    public void setColorMode(View v) {
    	mPreview.f_mode = 2;
    	displayFragment.dismiss();
    }
}